<script>
    import BaseMap from '@c/BaseMap/BaseMap'
    import {getPosistion, getNearByAddress,getAllAddress} from '@u/getUserPosition'//获取地理位置信息
    export default {
        data() {
            return {
                getPosistion,
                getNearByAddress,
                getAllAddress,
                routeInfo: {},
                currentCity: '',//当前城市
                currentPos: '',//当前位置
                nearByAddressList: [],//附近地址列表
                searchKey:'',//搜索的地址
            }
        },
        components: {
            BaseMap,
        },
        methods: {
            /**
             * @Description 重新获取定位
             * @author 焦政
             * @date 2019/4/28
             */
            async e_getLoaction() {
                let vue_this = this
                wx.getLocation({
                    type: 'wgs84',
                    async success(res) {
                        console.log('重新获取了', res)
                        let coordinate = {
                            longitude: res.longitude,
                            latitude: res.latitude
                        }
                        let posParams = await getPosistion(coordinate)
                        vue_this.$store.dispatch('SetRedeemData', posParams)
                        vue_this.currentPos = wx.getStorageSync('position')
                        vue_this.currentCity = wx.getStorageSync('city')
                        wx.switchTab({
                            url: '/pages/index/main'
                        })
                    }
                })
            },
            /**
             * @Description 自动 或搜索附近地址列表
             * @author 焦政
             * @date 2019/4/28
             */
            async autoGetNearByAddress(longitude, latitude, positionDes = '') {
                this.nearByAddressList = []
                this.nearByAddressList = await getNearByAddress(longitude, latitude, positionDes)
            },
            /**
             * @Description 搜索附近的地址
             * @author 焦政
             * @date 2019/4/28
             */
           async e_searchAddress(){
                this.nearByAddressList = []
                this.nearByAddressList = await getAllAddress(wx.getStorageSync('longitude'), wx.getStorageSync('latitude'),this.searchKey)

            },
            /**
             * @Description 选择一个附近的地址
             * @author 焦政
             * @date 2019/4/28
             */
            async e_selectAddress(v) {
                console.log(v.location.split(','))
                let coordinate = {
                    longitude: v.location.split(',')[0],
                    latitude: v.location.split(',')[1]
                }
                let posParams = await getPosistion(coordinate)
                this.$store.dispatch('SetRedeemData', posParams)
                this.currentPos = wx.getStorageSync('position')
                this.currentCity = wx.getStorageSync('city')
                wx.switchTab({
                    url: '/pages/index/main'
                })
                this.searchKey = ''
            },
        },
        onShow() {
            this.currentPos = wx.getStorageSync('position')
            this.currentCity = wx.getStorageSync('city')
            //    获取附近地址列表
            this.autoGetNearByAddress(wx.getStorageSync('longitude'), wx.getStorageSync('latitude'))
        },
        created() {
        }
    }
</script>
<template>
    <div class="currentAddress">
        <!--地图模式-->
        <!--        <BaseMap/>-->
        <!--        <map-route :route-info="routeInfo"></map-route>-->
        <!--搜索区域-->
        <div class="searchArea">
            <!--选择区域-->
            <div class="selectAddressArea">
                <img class="selectAddressIcon" src="/static/images/selectAddress.png" alt="">
                <span class="selectAddressLabel">{{currentCity}}</span>
                <img class="selectDownIcon" src="/static/images/down.png" alt="">
            </div>
            <!--搜索输入区域-->
            <div class="searchInput">
                <img class="searchIcon" src="/static/images/search.png" alt="">
                <input class="inputLabel" type="text" v-model="searchKey" placeholder="请输入地址" @input="e_searchAddress">
            </div>
        </div>
        <!-- 当前位置以及重新定位-->
        <div class="currentArea" @click="e_getLoaction">
            <span class="currentPos">{{currentPos}}</span>
            <div class="resetAddress">
                <img class="dingweiIcon" src="/static/images/dingwei.png" alt="">
                <span class="dingweiLabel">重新定位</span>
            </div>
        </div>
        <!-- 附近的地址-->
        <div class="addressArea">
            <div class="addressarea_label">
                <img class="addressarea_label_icon" src="/static/images/selectAddress.png" alt="">
                <span class="addressarea_label_title">附近的地址</span>
            </div>
            <div class="addressListArea" v-for="(v,i) in nearByAddressList" :key="i" @click="e_selectAddress(v)">
                <div class="listLabel">{{v.name}}</div>
            </div>
        </div>
        <!--添加收货地址区域-->
        <div class="addAddressCon">
            <img class="addAddressIcon" src="/static/images/addAddress.png" alt="">
            <span class="addressLabel">新增收货地址</span>
        </div>

    </div>
</template>
<style lang='scss'>
    @import "currentAddress";
</style>
